import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-20 15:41
 * 订单预约限制，如每日50人
 * 利用poi提供的api给java程序，office格式档案读和写功能。
 */
public class ReadFormExcel {

    @Test
    public void readFormExcel() throws Exception {
        //创建工作簿，构造方法文件路径
        Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\read.xlsx"));
        //获取工作表
        Sheet sheetAt = workbook.getSheetAt(0);

        //遍历工作表获取行对象
        for (Row row : sheetAt) {
            //遍历行对象，获取列的单元格
            for (Cell cell : row) {
                int cellType = cell.getCellType();
                if (Cell.CELL_TYPE_NUMERIC == cellType) {
                    //    判断数值类型的单元格
                    System.out.println(cell.getNumericCellValue() + ",");
                } else {
                    //    从单元格取值
                    System.out.println(cell.getStringCellValue() + ",");
                }
                //换行
                //System.out.println();
            }
            workbook.close();
        }
    }

    @Test
    public void exportExcel_lastRow() throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("D:\\read.xlsx"));
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for(int i=0;i<=lastRowNum;i++){
            //根据行号获取行对象
            XSSFRow row = sheet.getRow(i);
            //获取有数据的最后一列
            short lastCellNum = row.getLastCellNum();
            for(short j=0;j<lastCellNum;j++){
                String value = row.getCell(j).getStringCellValue();
                System.out.print(value+" ");
            }
            System.out.println();
        }
        workbook.close();
    }


}
